//app.js
App({
  onLaunch: function () {
    var self = this
    wx.checkSession({
      success() {
          console.log("已登录")
      },
      fail() {
        wx.login({
          success: res => {
            // 发送 res.code 到后台换取 openId, sessionKey, unionId
            if (res.code) {
              wx.request({
                url: self.globalData.baseUrl + '/yq/auth/openId', //接口地址
                method: "GET",
                data: {
                  "code": res.code,
                  "appId": self.globalData.appId
                },
                header: {
                  'content-type': 'application/json' // 默认值
                },
                success: function (res) {
                  //console.log(res)
                  var userInfo = res.data.info.user;
                  var unionId = res.data.info.unionId;
                  var openId = res.data.info.openId;
                  var token = res.data.info.token;
                  // wx.setStorageSync("openId", openId);
                  wx.setStorageSync("token", token);
                  if (res.data.info.openId) {
                    wx.setStorage({
                      key: "openId",
                      data: res.data.info.openId,
                    })
                  }
                }
              })
            }
          }
        })
      }
    })
  
    
    //wx.setStorageSync("openId", "oc2ls5ehaHUgNAyUTTiK_fDVZh9w");
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  globalData: {
    appId: 'KaYz1G87', // zhihui zhen-yee app id
    // appId: 'DoeqKYNP', // youqu
    // appId: 'v3RaWZzX', // youqu old
    userInfo: null,
    utils: require('utils/util.js'),
    baseUrl: 'https://release.zhen-yee.com'
    // baseUrl: 'http://192.168.50.18:9090'
  },

  request: function (url, params, success, fail, compl) {
    if (params == null) {
      params = {};
    }
    var timestamp = String(this.globalData.utils.accDiv(new Date().getTime(), 1000));
    if (timestamp.indexOf(".") >= 0) {
      timestamp = timestamp.split(".")[0];
    }
    params['appId'] = this.globalData.appId;
    params['openId'] = wx.getStorageSync("openId");
    params.timestamp = timestamp;
    params.sign = this.globalData.utils.genSign(params);
    wx.request({
      url: url,
      data: params,
      success: function (res) {
        if (res.data.status == 200) {
          if (success) {
            success(res.data);
          }
        }
        else if(res.data.status==401){
          wx.checkSession({
            success() {
              wx.navigateTo({
                url: '/pages/register/register',
              })
            },
            fail() {

            }
          })
        }   
        else {
          if (fail) {
            fail(res.data);
          }
          wx.showToast({
            title: String(res.data.msg),
            icon: 'none'
          })
        }
      },
      fail: function () {
        if (fail) {
          fail(null);
        }
        wx.showToast({
          title: '加载失败',
          icon: 'none'
        })
      },
      complete: function () {
        if (compl) {
          compl();
        }
      }
    })
  },
  
  requestLoading: function (url, params, success, fail, complete) {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    var s = function (res) {
      wx.hideLoading();
      if (success) {
        success(res);
      }
    };
    var f = function (res) {
      wx.hideLoading();
      if (fail) {
        fail(res);
      }
    };
    var c = function () {
      if (complete) {
        complete();
      }
    };
    this.request(url, params, s, f, c);
  }
})
